package com.project.rfidtodb.controller;

import com.project.rfidtodb.service.TimerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/timer")
public class TimerController {
    @Resource
    private TimerService timerService;
    private  static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,10,200,
            TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(2000));

    @GetMapping("/health")
    public String health() {
        Runtime runtime = Runtime.getRuntime();
        long usedMemory = runtime.totalMemory() - runtime.freeMemory();
        return String.format("Used Memory: %d MB", usedMemory / 1024 / 1024);
    }

    /**
     * 数据库处理
     */
    @RequestMapping("/dbProcess")
    public void dbProcess() {
        threadPoolExecutor.execute(()->{
            try {
                timerService.dbProcess();
            } catch (Exception e) {
                log.error("dbProcess error:{}",e);

            }
        });
    }
}
